﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public delegate void InputCallBack(string inputStr);

public class InputCtrl 
{
    private static InputCtrl mInstance = null;
    public static InputCtrl Instance
    {
        get
        {
            if (mInstance == null)
            {
                mInstance = new InputCtrl();
            }
            return mInstance;
        }
    }

    private readonly string prefabPath = "Prefabs/inputPanel";
    private GameObject mInputPanel = null;

    private InputCallBack callBack = null;
    private UIButton yesBtn = null;
    private UIButton noBtn = null;
    private UILabel titleLab = null;
    private UILabel inputLab = null;
    private UILabel yesLab = null;
    private UILabel noLab = null;
    private UIInput inputWidget = null;

    private InputCtrl()
    {
        var go = Resources.Load(prefabPath) as GameObject;
        mInputPanel = GameObject.Instantiate<GameObject>(go);
        mInputPanel.name = "InputCtrl";
        var trans = mInputPanel.transform;
        trans.SetParent(NGUITools.TryGetUIRoot().transform);
        trans.localScale = Vector3.one;
        trans.localPosition = Vector3.one;
        trans.localEulerAngles = Vector3.zero;

        InitPanel();
    }

    private void InitPanel()
    {
        var trans = mInputPanel.transform;
        yesBtn = trans.Find("bg/okBtn").GetComponent<UIButton>();
        noBtn = trans.Find("bg/cencleBtn").GetComponent<UIButton>();
        titleLab = trans.Find("bg/titleLab").GetComponent<UILabel>();
        yesLab = yesBtn.transform.Find("Label").GetComponent<UILabel>();
        noLab = noBtn.transform.Find("Label").GetComponent<UILabel>();
        inputWidget = trans.Find("inputWidget").GetComponent<UIInput>();
        inputLab = trans.Find("inputWidget/inputLab").GetComponent<UILabel>();


        yesBtn.onClick.Add(new EventDelegate(() =>
        {
            var text = inputLab.text;
            if (string.IsNullOrEmpty(text))
            {
                OnClickBtn(null);
            }
            else
            {
                OnClickBtn(text);
            }
            
        }));

        noBtn.onClick.Add(new EventDelegate(() =>
        {
            OnClickBtn(null);
        }));
    }

    private void OnClickBtn(string inputStr)
    {
        Close();
        if (this.callBack != null)
        {
            this.callBack(inputStr);
        }
    }

    public void Open(string title, InputCallBack callBack)
    {
        Open(title, null, null, null, callBack);
    }
    public void Open(string title, string content, string yesStr, string noStr, InputCallBack callBack)
    {
        SetPanel(title, content, yesStr, noStr);
        this.callBack = callBack;
        mInputPanel.SetActive(true);
    }

    private void SetPanel(string title, string content, string yesStr, string noStr)
    {
        titleLab.text = string.IsNullOrEmpty(title) ? "请输入" : title;
        inputWidget.value = "";
        inputLab.text = "";
        inputWidget.defaultText = string.IsNullOrEmpty(content) ? content : "点击这里开始输入";
        yesLab.text = string.IsNullOrEmpty(yesStr) ? "确定" : yesStr;
        noLab.text = string.IsNullOrEmpty(noStr) ? "取消" : noStr;
        
    }

    private void Close()
    {
        mInputPanel.SetActive(false);
    }
    
}
